'''
Created on Aug 19, 2009

@author: echeek
'''
import math

import primesieve

def permutationGroup(value):
    counts=[0]*10;
    while(value>0):
        counts[value%10]+=1;
        value=int(math.floor(value/10));
    return tuple(counts);   


def Euler49():
    primesraw=primesieve.listPrimesBasic(10000);
    primes=[];
    for x in primesraw:
        if(x>999):  #filter all but 4 digit
            primes.append(x);
            
    candidates=dict();
    
    for x in primes:
        group=permutationGroup(x);
        if(not group in candidates):
            candidates[group]=[];
        candidates[group].append(x);  
    
    for key in candidates.iterkeys():
        group=candidates[key];
        if(len(group)<3):
            continue;
        for ai in range( len(group) - 2 ):
            for bi in range(ai+1, len(group) -1):
                diff=group[bi]-group[ai];
                if( (group[ai]+2*diff) in group):
                    print "%s%s%s" % (str(group[ai]), str(group[ai]+diff), str(group[ai]+2*diff));
        


Euler49();
